package com.platform.controller;

import com.platform.entity.CartEntity;
import com.platform.entity.GoodsEntity;
import com.platform.entity.OrderGoodsEntity;
import com.platform.entity.ProductEntity;
import com.platform.service.CartService;
import com.platform.service.GoodsService;
import com.platform.service.OrderGoodsService;
import com.platform.service.ProductService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-30 14:31:21
 */
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderGoodsService orderGoodsService;
    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ProductEntity> productList = productService.queryList(query);
        int total = productService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(productList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:info")
    public R info(@PathVariable("id") Integer id) {
        ProductEntity product = productService.queryObject(id);

        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:save")
    public R save(@RequestBody ProductEntity product) {
        product.setIsOnSale(1);//是否下架：1-正常；0-下架
        productService.save(product);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:update")
    public R update(@RequestBody ProductEntity product) {
        productService.update(product);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:delete")
    public R delete(@RequestBody Integer[] ids) {

        if (ids != null) {
            for (Integer productId : ids) {
                ProductEntity productEntity = productService.queryObject(productId);

                Map<String, Object> params = new HashMap<>();
                params.put("productId", productId);
                List<OrderGoodsEntity> orderGoodsEntities = orderGoodsService.queryList(params);
                if (orderGoodsEntities.size() > 0) {
                    return R.error("产品名为: " + productEntity.getProductName() + " 有订单关联,不能删除");
                }

                List<CartEntity> cartEntities = cartService.queryListByCondition(params);
                if (cartEntities.size() > 0) {
                    return R.error("产品名为: " + productEntity.getProductName() + " 有购物车关联,不能删除");
                }

                List<GoodsEntity> goodsEntities = goodsService.queryList(params);
                if (goodsEntities.size() > 0) {
                    return R.error("产品名为: " + productEntity.getProductName() + " 是主产品,不能删除");
                }
            }
        }

        productService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 查看所有列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ProductEntity> list = productService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 根据goodsId查询商品
     *
     * @param goodsId
     * @return
     */
    @RequestMapping("/queryByGoodsId/{goodsId}")
    public R queryByGoodsId(@PathVariable("goodsId") String goodsId) {
        Map<String, Object> params = new HashMap<>();
        params.put("goodsId", goodsId);
        List<ProductEntity> list = productService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 上架
     */
    @RequestMapping("/enSale")
    public R enSale(@RequestBody Integer id) {

        ProductEntity productEntity = productService.queryObject(id);

        if (productEntity == null) {
            return R.error("该商品不存在");
        }


        if (productEntity.getIsOnSale() == 1) {
            return R.error("该商品已经是上架状态");
        }

        productService.enSale(id);

        Map<String, Object> param = new HashMap<>();
        param.put("productId", id);
        List<CartEntity> cartEntities = cartService.queryListByCondition(param);
        if (cartEntities.size() > 0) {
            for (CartEntity cartEntity : cartEntities) {
                cartEntity.setProductStatus(1);//设置为上架状态
                cartService.update(cartEntity);
            }
        }

        return R.ok();
    }

    /**
     * 下架
     */
    @RequestMapping("/unSale")
    public R unSale(@RequestBody Integer id) {
        ProductEntity productEntity = productService.queryObject(id);
        if (productEntity == null) {
            return R.error("该商品不存在");
        }

        if (productEntity.getIsOnSale() == 0) {
            return R.error("该商品已经是下架状态");
        }
        productService.unSale(id);

        //查询是否还有上架的产品，没有就把对应的商品设置为下架
        Map<String, Object> params = new HashMap<>();
        Integer goodsId = productEntity.getGoodsId();
        params.put("goodsId", goodsId);
        params.put("isOnSale", 1);
        List<ProductEntity> productEntityList = productService.queryList(params);
        if (productEntityList.size() == 0) {
            //把对应的商品设置为下架
            GoodsEntity goodsEntity = goodsService.queryObject(goodsId);
            goodsEntity.setIsOnSale(0);//设置该商品为下架
            goodsService.update(goodsEntity);
        }

        //查询购物车表，如果有数据则设置为不可用
        Map<String, Object> param = new HashMap<>();
        param.put("productId", id);
        List<CartEntity> cartEntities = cartService.queryListByCondition(param);

        if (cartEntities.size() > 0) {
            for (CartEntity cartEntity : cartEntities) {
                cartEntity.setProductStatus(0);//设置为下架状态
                cartService.update(cartEntity);
            }
        }

        return R.ok();
    }
}
